﻿/*
 * Project: IV:MP GUI Editor (http://code.google.com/p/ivmp-gui-editor)
 * Code license: GNU Lesser GPL (http://www.gnu.org/copyleft/lesser.html)
 * 
 * Authors: CrackHD
 * Description: 
 */

using System;
using System.Collections;
using System.Reflection;
using System.Windows;
using System.Windows.Media;

namespace IvmpEditor
{
    public static partial class CEGUI
    {
        public struct GUIProperty
        {
            public string Name;
            public object Value;

            public string ValueString
            {
                get
                {
                    // Parsing our name&value to string presentation
                    switch (Name)
                    {
                        case "NormalTextColour":
                        case "DisabledTextColour":
                        case "HoverTextColour":
                        case "PushedTextColour":
                            var color = (Value as SolidColorBrush).Color;
                            var colorStr = color.ToString();
                            colorStr = colorStr.Substring(1, colorStr.Length - 1);
                            return '\"' + colorStr + '\"';
                    }
                    throw new NotSupportedException("Unsupported property: " + Name);
                }
            }
        }
        public static GUIProperty ParseProperty(string parseString)
        {
            // Parsing property from string (name&value)
            var property = new GUIProperty();
            property.Name = parseString.Substring(0, parseString.IndexOf(';'));
            var i = property.Name.Length + 1;
            var value = parseString.Substring(i, parseString.Length - i);

            switch (property.Name)
            {
                // Colors parsed to WPF's SolidColorBrush
                case "NormalTextColour":
                case "DisabledTextColour":
                case "HoverTextColour":
                case "PushedTextColour":
                    var color = (Color)ColorConverter.ConvertFromString(value);
                    property.Value = new SolidColorBrush(color);
                    break;

                default:
                    throw new NotSupportedException("Unsupported property: " + property.Name);
            }

            return property;
        }
        public static void ApplyProperty(Style processingStyle, GUIProperty property)
        {
            // Applies property to WPF style:
            SetDictionaryValue(processingStyle, 'x' + property.Name, property.Value);
        }
        public static Style CopyStyle(Style src)
        {
            Style basedOn = null;
            if (src.BasedOn != null)
                basedOn = CopyStyle(src.BasedOn);
            Style style = new Style(src.TargetType, basedOn);

            // Copy resources (full access):
            foreach (var rd in src.Resources)
            {
                DictionaryEntry de = (DictionaryEntry)rd;
                style.Resources.Add(de.Key, de.Value);
            }

            // Copy MergedDictionaries
            foreach (ResourceDictionary md in src.Resources.MergedDictionaries)
            {
                ResourceDictionary d = new ResourceDictionary();
                foreach (var v in md)
                    d.Add(v, md[v]);
                style.Resources.MergedDictionaries.Add(d);
            }

            // Copy setters:
            foreach (Setter s in src.Setters)
                style.Setters.Add(new Setter(s.Property, s.Value, s.TargetName));

            // Copy triggers (read-only):
            foreach (var t in src.Triggers)
                style.Triggers.Add(t);

            return style;
        }
        public static object GetDictionaryValue(Style s, string key, object defaultVal)
        {
            if (s.Resources != null && s.Resources[key] != null)
                return s.Resources[key];
            if (s.Resources.MergedDictionaries != null)
                foreach (var rd in s.Resources.MergedDictionaries)
                    foreach (DictionaryEntry de in rd.Keys)
                        if ((string)de.Key == key)
                            return de.Value;
            if (s.BasedOn != null)
                return GetDictionaryValue(s.BasedOn, key, defaultVal);

            return defaultVal;
        }
        public static void SetDictionaryValue(Style s, string key, object val)
        {
            if (s.Resources != null && s.Resources[key] != null)
            {
                s.Resources[key] = val;
                return;
            }
            if (s.Resources.MergedDictionaries != null)
            {
                int rdi = 0, dei = 0;
                foreach (ResourceDictionary rd in s.Resources.MergedDictionaries)
                {
                    foreach (DictionaryEntry de in rd)
                        if ((string)((DictionaryEntry)de.Key).Key == key)
                        {
                            s.Resources.MergedDictionaries[rdi][(string)key] = val;
                            return;
                        }
                        else dei++;
                    rdi++;
                }
            }
            if (s.BasedOn != null)
            {
                SetDictionaryValue(s.BasedOn, key, val);
                return;
            }

            throw new ArgumentException("not found dictionary value: " + key, "key");
        }
    }
}